<template>
  <view class="agentIndex" ref="container">
	<!--  -->
	<Groupon  :detail="combinationList" />
	

    <view class="nav acea-row row-middle">
      <view class="item2" :class="selected==0?'selected':''" @click="set_where(0)">待审核</view>
      <view class="item2" :class="selected==1?'selected':''" @click="set_where(1)">待付款</view>
      <view class="item2" :class="selected==2?'selected':''" @click="set_where(2)"> 待发货</view>
      <view class="item2" :class="selected==3?'selected':''" @click="set_where(3)">待收货</view>
	  <view class="item2" :class="selected==4?'selected':''" @click="set_where(4)">已完成</view>
	  <view class="item2" :class="selected==5?'selected':''" @click="set_where(5)">已取消</view>
    </view>
	<view class="searchBox">
		<input type="text" placeholder="  输入药房名" v-model="username"  />
		<image src="/static/images/search.png" @click="getOrderListByIsVerify()"></image>
	</view>
	<view class="yflist" v-for="(yf,yfListIndex) in yfList" :key="yfListIndex">
		 <view class="yfList_title">{{yf}}</view>
		 <view  v-for="(order,orderListIndex) in orderList" :key="orderListIndex">
			 <view class="yfList_order_view" v-if="order.realName==yf">
				  <view class="pictrue">
					<image :src="order.cartInfo[0].productInfo.image" />
				  </view>
				 <view class="title_view" >
				   <view class="title">{{ order.cartInfo[0].productInfo.storeName }}</view>
				 </view>
				 <view class="title_price">
				   <view class="money font-color-red" :class="Switch === true ? '' : 'on'">
				     ￥
				     <text class="num">{{ order.cartInfo[0].productInfo.price }}</text>
				   </view>
				   <view class="specs line1">每片重0.05g</view>
				   <view class="specs line1">共{{order.totalNum}}件商品</view>
				   <view class="verify_btn" @click="goToOrderVerify(order)">去审核</view>
				 </view>
				 
			 </view>
		 </view>
	</view>
	
   <tabBar :pagePath="'/pages/agent/Index/index'"></tabBar>
  </view>
</template>
<script>
import Recommend from '@/components/Recommend'
import Groupon from "@/components/sh-groupon.vue"
import { getProducts, getProductsIntegral } from '@/api/store'
import { getUserList } from '@/api/user'
import Loading from '@/components/Loading'
import { getOrderData, getOrderList, getOrderListByIsVerify } from "@/api/order";
import Banner from "../../home/components/Banner";
import {
		getHomeData,
		getShare,
		getCanvas
	} from "@/api/public";
export default {
  name: 'GoodsList',
  components: {
    Recommend,
	Banner,
	Groupon,
    Loading,
  },
  props: {},
  data: function () {
    // const { s = "", id = 0, title = "" } = this.$yroute.query;
    const s = '',
      id = 0,
      title = ''

    return {
	  homeData: [],
	  combinationList: [],
      hostProduct: [],
      productList: [],
	  orderList: [],
	  yfList:[],//药房list
      Switch: true,
	  selected:0,
	  username:'',
	  page: 1,
	  limit: 20,
	  isVerify:0,
	  realName:'all',
      where: {
        page: 1,
        limit: 8,
        keyword: s,
        sid: id, //二级分类id
        news: 0,
        priceOrder: '',
        salesOrder: '',
      },
      title: title && id ? title : '',
      loadTitle: '',
      loading: false,
      loadend: false,
      price: 0,
      stock: 0,
      nows: false,
      recommendLoading: false,
      target: false,
      isIntegral: false,
    }
  },
  onLoad: function() {
  
  	let that = this;
  	// uni.showLoading({
  	//   title: "加载中",
  	// });
  	getCanvas().then(res => {}).catch(error => {
  		this.homeData = JSON.parse(error.data.json)
  		console.log(this.homeData)
  		console.log(222)
  	})
	getUserList(1).then((res) => {
		console.log("---------------userlist--------------------");
		console.log(res);
  		
  		// that.doColorThief()
  	});
  	getHomeData().then((res) => {
		console.log("---------------******************--------------------");
		console.log(res);
  		res.data.banner.map((item) => (item.bgcolor = item.color || ""));
		that.$set(that, "combinationList", res.data.combinationList);
		console.log(that.combinationList);
  		// that.doColorThief()
  	});
	that.getOrderListByIsVerify();
  },
  watch: {
    title() {
      this.updateTitle()
    },
    isIntegral() {
      if (this.isIntegral) {
        uni.setNavigationBarTitle({
          title: '积分商品',
        })
      } else {
        uni.setNavigationBarTitle({
          title: '商品列表',
        })
      }
    },
    $yroute(to) {
      // if (to.name !== "GoodsList") return;
      // const { s = "", id = 0, title = "" } = to.query;
      // if (s !== this.where.keyword || id !== this.where.sid) {
      //   this.where.keyword = s;
      //   this.loadend = false;
      //   this.loading = false;
      //   this.where.page = 1;
      //   this.where.sid = id;
      //   this.title = title && id ? title : "";
      //   this.nows = false;
      //   this.$set(this, "productList", []);
      //   this.price = 0;
      //   this.stock = 0;
      //   this.getProductList();
      // }
    },
  },
  mounted: function () {
    const { s = '', id = 0, title = '', isIntegral = false } = this.$yroute.query

    this.where.keyword = s
    this.where.isIntegral = isIntegral == 'true' ? 1 : 0
    this.isIntegral = isIntegral
    this.updateTitle()
    this.getProductList()
  },
  onReachBottom() {
    this.recommendLoading = true
    !this.loading && this.getProductList()
  },
  onHide() {
    // this.hostProduct = [];
    // this.productList = [];
    // this.Switch = true;
    // this.where = {
    //   page: 1,
    //   limit: 8,
    //   keyword: s,
    //   sid: id, //二级分类id
    //   news: 0,
    //   priceOrder: "",
    //   salesOrder: ""
    // };
    // this.loadTitle = "";
    // this.loading = false;
    // this.loadend = false;
    // this.price = 0;
    // this.stock = 0;
    // this.nows = fals;
  },
  methods: {
    changeRecommendLoading(recommendLoading) {
      this.recommendLoading = recommendLoading
    },
    goGoodsCon(item) {
      this.$yrouter.push({
        path: '/pages/shop/GoodsCon/index',
        query: {
          id: item.id,
          isIntegral: this.isIntegral,
        },
      })
    },
	getOrderListByIsVerify() {
	  
	  this.realName=this.username;
	  const { page, limit, isVerify,realName } = this;
	  getOrderListByIsVerify({
	    page,
	    limit,
	    isVerify,
		realName
	  }).then(res => {
		 console.log("-----------------OrderListByIsVerify--------------------");
	     console.log(res);
		 this.orderList=res.data;
			 
		 for(var i=0;i<res.data.length;i++){
			 this.yfList.push(res.data[i].realName);
		 }
		
		 var temp = []; //一个新的临时数组
		     for(var i = 0; i < this.yfList.length; i++){
		         if(temp.indexOf(this.yfList[i]) == -1){
		             temp.push(this.yfList[i]);
		         }
		  }
		 this.yfList=temp;
		 console.log(this.yfList);
		  console.log("-----------------OrderListByIsVerify---end-----------------");
	  });
	},
    goToOrderVerify(item) {
      this.$yrouter.push({
        path: '/pages/agent/Order/OrderDetails/index',
        query: {
          id: item.orderId
        },
      })
    },
    updateTitle() {
      /*uni.setNavigationBarTitle({
        title: this.title,
      })*/
      // document.title = this.title || this.$yroute.meta.title;
    },
    getProductList() {
      var that = this
      this.setWhere()
      // if (to.name !== "GoodsList") return;
      const { s = '', id = 0, title = '' } = this.$yroute.query
      if (s !== this.where.keyword || id !== this.where.sid) {
        this.loadend = false
        this.loading = false
        this.where.page = 1
        this.where.sid = id
        this.title = title && id ? title : ''
        this.nows = false
        this.$set(this, 'productList', [])
        this.price = 0
        this.stock = 0
        // this.getProductList();
      }
      let q = that.where
      let getData = this.isIntegral !== 'true' ? getProducts : getProductsIntegral
      getData(q).then(res => {
		  console.log(res.data);
        that.loading = false
        if (that.target) {
          that.productList = res.data
        } else {
          that.productList.push.apply(that.productList, res.data)
        }
        that.target = false
        that.loadend = res.data.length < that.where.limit //判断所有数据是否加载完成；
        that.where.page = that.where.page + 1
      })
    },
    submitForm: function () {
      this.$set(this, 'productList', [])
      this.where.page = 1
      this.loadend = false
      this.loading = false
      this.getProductList()
    },
    //点击事件处理
    set_where: function (index) {
      let that = this
	  that.selected=index;
      switch (index) {
        case 0:
         
        case 1:
         
          break
        case 2:
          
          break
        case 3:
          break
        default:
          break
      }
      this.productList = []
      that.$set(that, 'productList', [])
      that.where.page = 1
      that.target = true
      that.loadend = false
      that.getProductList()
    },
    //设置where条件
    setWhere: function () {
      let that = this
      if (that.price === 0) {
        that.where.priceOrder = ''
      } else if (that.price === 1) {
        that.where.priceOrder = 'asc'
      } else if (that.price === 2) {
        that.where.priceOrder = 'desc'
      }
      if (that.stock === 0) {
        that.where.salesOrder = ''
      } else if (that.stock === 1) {
        that.where.salesOrder = 'asc'
      } else if (that.stock === 2) {
        that.where.salesOrder = 'desc'
      }
      that.where.news = that.nows ? '1' : '0'
    },
    switchTap: function () {
      let that = this
      that.Switch = !that.Switch
    },
  },
}
</script>
<style scoped lang="less">
.noCommodity {
  border-top: 3px solid #f5f5f5;
  padding-bottom: 1px;
}
</style>
